//
//  MLToolBar.swift
//  MLSina20160624
//
//  Created by manmanlang on 16/7/3.
//  Copyright © 2016年 manmanlang. All rights reserved.
//

import UIKit

enum ToolBarType:Int {
    case Picture = 101
    case Mention = 102
    case Trend = 103
    case Emoticon = 104
    case Add = 105
}
class MLToolBar: UIView {
    
    //通过闭包告知外界按钮被点击 闭包用var修饰
    var closure:((type:ToolBarType)->())?
    
    var index:CGFloat = 0
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        self.backgroundColor = UIColor.whiteColor()
        addClildButton("compose_toolbar_picture", type: ToolBarType.Picture)
        addClildButton("compose_mentionbutton_background", type: ToolBarType.Mention)
        addClildButton("compose_trendbutton_background", type: ToolBarType.Trend)
        addClildButton("compose_emoticonbutton_background", type: ToolBarType.Emoticon)
        addClildButton("compose_add_background", type: ToolBarType.Add)
        
    }
    
    func addClildButton(imageName: String, type: ToolBarType){
        let button:UIButton = UIButton(type: UIButtonType.Custom)
        button.backgroundColor = RandomColor()
        addSubview(button)
        button.tag = type.rawValue
        let backgroundImageName = "compose_toolbar_background"
        button.addTarget(self, action: #selector(MLToolBar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: backgroundImageName), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: backgroundImageName), forState: UIControlState.Highlighted)
        
        let buttonW = UIScreen_Bounds_Width / 5
        
        button.snp_makeConstraints(closure: { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(self).offset(index * buttonW)
            make.width.equalTo(buttonW)
            index += 1
        })
        
    }
    
    func buttonClick(button:UIButton){
        closure?(type: ToolBarType(rawValue: button.tag)!)
    }
    
}
